#include <QtGui>
#include <QTranslator>
#include <QtCore/QTextCodec>
#include "gv_systemcomconfigurationdialog.h"
#include "gv_home.h"
#include "gv_systemselectiondialog.h"
#include "gv_setting.h"

extern GV_Setting *gvsetting;
extern QDialog  *dialog;
GV_SystemCOMConfigurationDialog::GV_SystemCOMConfigurationDialog(QWidget *parent)
       : QDialog(parent)
{
    setupUi(this);

    QTranslator translator;
    translator.load(gvsetting->GV_GetLanguage(),".");
    qApp->installTranslator(&translator);
    reloadtext();

    TimeHintsLabel->setText((QDateTime::currentDateTime().toString("hh:mm:ss")));
    DateHintsLabel->setText((QDateTime::currentDateTime().toString("yyyy-MM-dd")));

    ParityFrame->hide();
    StopbitsFrame->hide();
    DatabitsFrame->hide();
    DetailedviewFrame->hide();

    parityEnabled = PARITYNONEENABLED;
    databitsEnabled = DATABITS7ENABLED;
    stopbitsEnabled = STOPBITS1ENABLED;
    bandrate = 9600;

    timer = new QTimer;
    connect(timer,SIGNAL(timeout()),this,SLOT(GV_chTime()));
    connect(AcceptButton,SIGNAL(clicked()),this,SLOT(accept()));
    connect(CancelButton,SIGNAL(clicked()),this,SLOT(cancel()));

    connect(BandrateButton,SIGNAL(clicked()),this,SLOT(bandrateclicked()));
    connect(ParityButton,SIGNAL(clicked()),this,SLOT(parityclicked()));
    connect(DatebitsButton,SIGNAL(clicked()),this,SLOT(databitsclicked()));
    connect(StopbitsButton,SIGNAL(clicked()),this,SLOT(stopbitsclicked()));
    connect(DetailedviewButton,SIGNAL(clicked()),this,SLOT(detailedviewclicked()));

    connect(BandratelistWidget,SIGNAL(itemSelectionChanged()),this,SLOT(bandrateselected()));

    connect(Databits7Radio,SIGNAL(clicked()),this,SLOT(parityframeselected()));
    connect(Databits8Radio,SIGNAL(clicked()),this,SLOT(parityframeselected()));

    connect(Stopbit1Radio,SIGNAL(clicked()),this,SLOT(databitsframeselected()));
    connect(Stopbit2Radio,SIGNAL(clicked()),this,SLOT(databitsframeselected()));

    connect(NoneRadio,SIGNAL(clicked()),this,SLOT(stopbitsframeselected()));
    connect(OddRadio,SIGNAL(clicked()),this,SLOT(stopbitsframeselected()));
    connect(EvenRadio,SIGNAL(clicked()),this,SLOT(stopbitsframeselected()));


    timer->start(1000);
}
void GV_SystemCOMConfigurationDialog::reloadtext()
{
    //NameHintsLabel->setText(tr("Zones"));
}
void GV_SystemCOMConfigurationDialog::GV_chTime()
{
    TimeHintsLabel->setText((QDateTime::currentDateTime().toString("hh:mm:ss")));
    DateHintsLabel->setText((QDateTime::currentDateTime().toString("yyyy-MM-dd")));
}

void GV_SystemCOMConfigurationDialog::home()
{
    QDialog *dialog1 = new GV_SystemSelectionDialog;
    dialog1->show();
    delete dialog;
    dialog = dialog1;
}
void GV_SystemCOMConfigurationDialog::accept()
{
    /*
    QMessageBox::about(this,
                       "COMConfiguration",
                       "Band Rate:" + QString::number(bandrate) +
                       " Parity:" + QString::number(parityEnabled) +
                        " Data Bits:" + QString::number(databitsEnabled) +
                        " Stop Bits:" + QString::number(stopbitsEnabled));
                        */
    QDialog *dialog1 = new GV_SystemSelectionDialog;
    dialog1->show();
    delete dialog;
    dialog = dialog1;
}
void GV_SystemCOMConfigurationDialog::cancel()
{
    QDialog *dialog1 = new GV_SystemSelectionDialog;
    dialog1->show();
    delete dialog;
    dialog = dialog1;
}

void GV_SystemCOMConfigurationDialog::bandrateclicked()
{
    BandratelistWidget->show();
    ParityFrame->hide();
    StopbitsFrame->hide();
    DatabitsFrame->hide();
    DetailedviewFrame->hide();
}

void GV_SystemCOMConfigurationDialog::parityclicked()
{
    BandratelistWidget->hide();
    ParityFrame->show();
    StopbitsFrame->hide();
    DatabitsFrame->hide();
    DetailedviewFrame->hide();
}
void GV_SystemCOMConfigurationDialog::databitsclicked()
{
    BandratelistWidget->hide();
    ParityFrame->hide();
    StopbitsFrame->hide();
    DatabitsFrame->show();
    DetailedviewFrame->hide();
}
void GV_SystemCOMConfigurationDialog::stopbitsclicked()
{
    BandratelistWidget->hide();
    ParityFrame->hide();
    StopbitsFrame->show();
    DatabitsFrame->hide();
    DetailedviewFrame->hide();
}
void GV_SystemCOMConfigurationDialog::detailedviewclicked()
{
    char namelist[][15]={"None","Odd","Even",
                        "7 Data Bits","8 Data Bits",
                        "1 Stop Bits","2 Stop Bits"
    };
    BandratelistWidget->hide();
    ParityFrame->hide();
    StopbitsFrame->hide();
    DatabitsFrame->hide();


    DetailedviewFrame->setGeometry(QRect(20,80,221,131));
    DetailedviewFrame->show();

    BandrateValueLabel->setText(QString::number(bandrate));
    ParityValueLabel->setText(namelist[parityEnabled]);
    DatabitsValueLabel->setText(namelist[databitsEnabled]);
    StopbitValueLabel->setText(namelist[stopbitsEnabled]);

}
void GV_SystemCOMConfigurationDialog::bandrateselected()
{
    int bandratelist[]={300,600,1200,2400,4800,9600,14400,56000,57600,115200};
    bandrate =bandratelist[BandratelistWidget->currentRow()];

}
void GV_SystemCOMConfigurationDialog::parityframeselected()
{

    if(NoneRadio->isChecked()){
        parityEnabled = PARITYNONEENABLED;
    }else if(OddRadio->isChecked()){
        parityEnabled = PARITYODDENABLED;
    }else if(EvenRadio->isChecked()){
        parityEnabled = PARITYEVENENABLED;
    }
}
void GV_SystemCOMConfigurationDialog::databitsframeselected()
{
    if(Databits7Radio->isChecked()){
        databitsEnabled = DATABITS7ENABLED;
    }else if(Databits8Radio->isChecked()){
        databitsEnabled = DATABITS8ENABLED;
    }
}
void GV_SystemCOMConfigurationDialog::stopbitsframeselected()
{
    if(Stopbit1Radio->isChecked()){
        stopbitsEnabled = STOPBITS1ENABLED;
    }else if(Stopbit2Radio->isChecked()){
        stopbitsEnabled = STOPBITS2ENABLED;
    }
}

